home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / modes / sendmail.el < prev    next >
Encoding:
Text File  |  1995-08-22  |  37.1 KB  |  1,056 lines

  1. ;;; sendmail.el --- mail sending commands for Emacs.
  2.  
  3. ;; Copyright (C) 1985, 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: mail
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; This mode provides mail-sending facilities from within Emacs.  It is
  28. ;; documented in the Emacs user's manual.
  29.  
  30. ;;; Code:
  31.  
  32. ;;;###autoload
  33. (defvar mail-self-blind nil "\
  34. Non-nil means insert BCC to self in messages to be sent.
  35. This is done when the message is initialized,
  36. so you can remove or alter the BCC field to override the default.")
  37.  
  38. ;;;###autoload
  39. (defvar mail-interactive nil "\
  40. Non-nil means when sending a message wait for and display errors.
  41. nil means let mailer mail back a message to report errors.")
  42.  
  43. ;;;###autoload
  44. (defvar mail-dir nil "*Default directory for saving messages.")
  45.  
  46. ;;; XEmacs change: moved rmail-ignored-headers here from rmail.el so that
  47. ;;; the value of mail-yank-ignored-headers can default from it.  Both of
  48. ;;; these end up in loaddefs.el, but "sendmail" comes before "rmail", so...
  49. ;;;
  50. ;;;###autoload
  51. (defvar rmail-ignored-headers 
  52.   (purecopy
  53.    (concat
  54.     "^\\("
  55.     (mapconcat
  56.      'identity
  57.      '(;; RFC822
  58.        "Sender:" "References:" "Return-Path:" "Received:"
  59.        "[^: \t\n]*Message-ID:" "Errors-To:"
  60.        ;; RFC977 (NNTP)
  61.        "Path:" "Expires:" "Xref:" "Lines:" "Approved:" "Distribution:"
  62.        ;; SYSV mail:
  63.        "Content-Length:"
  64.        ;; MIME:
  65.        "Mime-Version:" "Content-Type:" "Content-Transfer-Encoding:"
  66.        ;; X400
  67.        "X400-Received:" "X400-Originator:" "X400-Mts-Identifier:"
  68.        "X400-Content-Type:" "Content-Identifier:"
  69.        ;; RMAIL and /usr/ucb/mail:
  70.        "Status:" "Summary-Line:"
  71.        ;; Supercite:
  72.        "X-Attribution:"
  73.        ;; Other random junk occasionally seen:
  74.        "Via:" "Sent-Via:" "Mail-From:" "Origin:" "Comments:" "Originator:"
  75.        "NF-ID:" "NF-From:" "Posting-Version:" "Posted:" "Posted-Date:"
  76.        "Date-Received:" "Relay-Version:" "Article-I\\.D\\.:" "NNTP-Version:"
  77.        "NNTP-Posting-Host:" "X-Mailer:" "X-Newsreader:" "News-Software:"
  78.        "X-Received:" "X-References:" "X-Envelope-To:"
  79.        "X-VMS-" "Remailed-" "X-Plantation:" "X-Windows:" "X-Pgp-"
  80.        )
  81.      "\\|")
  82.     "\\)"))
  83.   "*Gubbish header fields one would rather not see.")
  84.  
  85.  
  86. ;;;###autoload
  87. (defvar mail-yank-ignored-headers
  88.   (purecopy
  89.    (concat rmail-ignored-headers "\\|"
  90.        "^\\("
  91.        (mapconcat 'identity
  92.               '(;; RFC822
  93.             "Resent-To:" "Resent-By:" "Resent-CC:"
  94.             "To:" "Subject:" "In-Reply-To:"
  95.             )
  96.               "\\|")
  97.        "\\)"))
  98.   "Delete these headers from old message when it's inserted in a reply.")
  99. ;; minimalist FSF version
  100. ;(defvar mail-yank-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
  101. ;Delete these headers from old message when it's inserted in a reply.")
  102.  
  103. ;; Useful to set in site-init.el
  104. ;;;###autoload
  105. (defvar send-mail-function 'sendmail-send-it "\
  106. Function to call to send the current buffer as mail.
  107. The headers are be delimited by a line which is `mail-header-separator'.")
  108.  
  109. ;;;###autoload
  110. (defvar mail-header-separator (purecopy "--text follows this line--") "\
  111. *Line used to separate headers from text in messages being composed.")
  112.  
  113. ;;;###autoload
  114. (defvar mail-archive-file-name nil "\
  115. *Name of file to write all outgoing messages in, or nil for none.
  116. Do not use an rmail file here!  Instead, use its inbox file.")
  117.  
  118. (defvar mail-default-reply-to nil
  119.   "*Address to insert as default Reply-to field of outgoing messages.")
  120.  
  121. ;;; FSFmacs randomness
  122. ;;(defvar mail-alias-file nil
  123. ;;  "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
  124. ;;This file defines aliases to be expanded by the mailer; this is a different
  125. ;;feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
  126. ;;This variable has no effect unless your system uses sendmail as its mailer.")
  127. ;;
  128. ;;(defvar mail-aliases t
  129. ;;  "Alist of mail address aliases,
  130. ;;or t meaning should be initialized from `~/.mailrc'.
  131. ;;The alias definitions in `~/.mailrc' have this form:
  132. ;;  alias ALIAS MEANING")
  133.  
  134. ;;;###autoload
  135. (defvar mail-yank-prefix "> "        ; XEmacs
  136.   "*Prefix insert on lines of yanked message being replied to.
  137. nil means use indentation.")
  138.  
  139. (defvar mail-indentation-spaces 3
  140.   "*Number of spaces to insert at the beginning of each cited line.
  141. Used by `mail-yank-original' via `mail-indent-citation'.")
  142.  
  143. (defvar mail-yank-hooks nil
  144.   "Obsolete hook for modifying a citation just inserted in the mail buffer.
  145. Each hook function can find the citation between (point) and (mark t).
  146. And each hook function should leave point and mark around the citation
  147. text as modified.
  148.  
  149. This is a normal hook, misnamed for historical reasons.
  150. It is semi-obsolete and mail agents should no longer use it.")
  151.  
  152. (defvar mail-citation-hook nil
  153.   "*Hook for modifying a citation just inserted in the mail buffer.
  154. Each hook function can find the citation between (point) and (mark t).
  155. And each hook function should leave point and mark around the citation
  156. text as modified.
  157.  
  158. If this hook is entirely empty (nil), a default action is taken
  159. instead of no action.")
  160.  
  161. (defvar mail-abbrevs-loaded nil)
  162. (defvar mail-mode-map nil)
  163.  
  164. ;;;###autoload
  165. (defvar mail-signature nil
  166.   "*Text inserted at end of mail buffer when a message is initialized.
  167. If t, it means to insert the contents of the file `mail-signature-file'.")
  168.  
  169. ;;#### Glorp!  Do we really need both of these losers?
  170. (defvar mail-insert-signature nil
  171.   "If t, automatically insert the contents of the file
  172. `mail-signature-file' before sending a message.")
  173.  
  174. (defvar mail-signature-file "~/.signature"
  175.   "File to be inserted at the end of a message. Usually, this file is called
  176. \"~/.signature\".")
  177.  
  178. (defvar mail-signature-inserted nil
  179.   "Non-nil means signature already inserted; don't reinsert it.")
  180.  
  181. (defvar mail-reply-buffer nil)
  182. (defvar mail-send-actions nil
  183.   "A list of actions to be performed upon successful sending of a message.")
  184.  
  185. (defvar mail-default-headers nil
  186.   "*A string containing header lines, to be inserted in outgoing messages.
  187. It is inserted before you edit the message,
  188. so you can edit or delete these lines.")
  189.  
  190. (defvar mail-mode-syntax-table nil
  191.   "Syntax table used while in mail mode.")
  192.  
  193. (if (null mail-mode-syntax-table)
  194.     (progn
  195.      (setq mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
  196.      (modify-syntax-entry ?% ". " mail-mode-syntax-table)))
  197.  
  198. (autoload 'mail-aliases-setup "mail-abbrevs")
  199.  
  200. (defun mail-setup (to subject in-reply-to cc replybuffer actions)
  201.   (make-local-variable 'mail-signature-inserted)
  202.   (setq mail-signature-inserted nil)
  203.   (setq mail-send-actions actions)
  204.   (mail-aliases-setup)
  205.   (setq mail-reply-buffer replybuffer)
  206.   (goto-char (point-min))
  207.   (insert "To: ")
  208.   (save-excursion
  209.     (if to
  210.         ;; Here removed code to extract names from within <...>
  211.         ;; on the assumption that mail-strip-quoted-names
  212.         ;; has been called and has done so.
  213.     (let ((fill-prefix "\t")
  214.               (address-start (point)))
  215.       (insert to "\n")
  216.           (fill-region-as-paragraph address-start (point-max)))
  217.       (newline))
  218.     (if cc
  219.     (let ((fill-prefix "\t")
  220.           (address-start (progn (insert "CC: ") (point))))
  221.       (insert cc "\n")
  222.       (fill-region-as-paragraph address-start (point-max))))
  223.     (if in-reply-to
  224.     (insert "In-reply-to: " in-reply-to "\n"))
  225.     (insert "Subject: " (or subject "") "\n")
  226.     (if mail-default-headers
  227.     (insert mail-default-headers))
  228.     (if mail-default-reply-to
  229.     (insert "Reply-to: " mail-default-reply-to "\n"))
  230.     (if mail-self-blind
  231.     (insert "BCC: " (user-login-name) "\n"))
  232.     (if mail-archive-file-name
  233.     (insert "FCC: " mail-archive-file-name "\n"))
  234.     (insert mail-header-separator "\n")
  235.  
  236.     ;; Insert the signature.  But remember the beginning of the message.
  237.     (if to (setq to (point)))
  238.     (cond ((eq mail-signature t)
  239.            (if (file-exists-p mail-signature-file)
  240.                (insert-file-contents mail-signature-file))
  241.            (setq mail-signature-inserted t))
  242.           (mail-signature
  243.            (insert mail-signature)
  244.            (setq mail-signature-inserted t)))
  245.     (goto-char (point-max))
  246.     (or (bolp) (newline)))
  247.   (if to (goto-char to))
  248.   (or to subject in-reply-to
  249.       (set-buffer-modified-p nil))
  250.   (run-hooks 'mail-setup-hook))
  251.  
  252. ;;;###autoload
  253. (defun mail-mode ()
  254.   "Major mode for editing mail to be sent.
  255. Like Text Mode but with these additional commands:
  256. C-c C-s  mail-send (send the message)    C-c C-c  mail-send-and-exit
  257. C-c C-f  move to a header field (and create it if there isn't):
  258.      C-c C-f C-t  move to To:    C-c C-f C-s  move to Subj:
  259.      C-c C-f C-b  move to BCC:    C-c C-f C-c  move to CC:
  260.      C-c C-f C-r  move to Reply-To:
  261. C-c C-t  move to message text.
  262. C-c C-y  mail-yank-original (insert current message, in Rmail).
  263. C-c C-w  mail-signature (insert signature file).
  264. C-c C-q  mail-fill-yanked-message (fill what was yanked).
  265. C-c C-v  mail-sent-via (add a sent-via field for each To or CC)
  266.  
  267. Button3  Popup menu with the above commands."
  268.   (interactive)
  269.   (kill-all-local-variables)
  270.   (make-local-variable 'mail-reply-buffer)
  271.   (setq mail-reply-buffer nil)
  272.   (make-local-variable 'mail-send-actions)
  273.   (make-local-variable 'mail-signature-inserted)
  274.   (set-syntax-table mail-mode-syntax-table)
  275.   (use-local-map mail-mode-map)
  276.   (setq local-abbrev-table text-mode-abbrev-table)
  277.   (setq major-mode 'mail-mode)
  278.   (setq mode-name "Mail")
  279.   (setq buffer-offer-save t)
  280.   (turn-on-auto-fill)            ; XEmacs - maybe filladapt should be default, too.
  281.   (make-local-variable 'paragraph-separate)
  282.   (make-local-variable 'paragraph-start)
  283.   (setq paragraph-start (concat "^" mail-header-separator
  284.                 "$\\|^[ \t]*[-_][-_][-_]+$\\|"
  285.                 paragraph-start))
  286.   (setq paragraph-separate (concat "^" mail-header-separator
  287.                    "$\\|^[ \t]*[-_][-_][-_]+$\\|"
  288.                    paragraph-separate))
  289.   (run-hooks 'text-mode-hook 'mail-mode-hook))
  290.  
  291.  
  292. ;;; Set up keymap.
  293.  
  294. (if mail-mode-map
  295.     nil
  296.   (setq mail-mode-map (make-sparse-keymap))
  297.   (set-keymap-parent mail-mode-map text-mode-map)
  298.   (set-keymap-name mail-mode-map 'mail-mode-map)
  299.   (define-key mail-mode-map "\C-c?" 'describe-mode)
  300.   (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
  301.   (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
  302.   (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
  303.   (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
  304.   (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
  305.   (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-replyto)
  306.   (define-key mail-mode-map "\C-c\C-t" 'mail-text)
  307.   (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
  308.   (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
  309.   (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
  310.   ;;CRAP!!(define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)CRAP!
  311.   (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
  312.   (define-key mail-mode-map "\C-c\C-s" 'mail-send)
  313.   (define-key mail-mode-map 'button3   'mail-mode-menu))
  314.  
  315. ;;; mail-mode popup menu
  316.  
  317. (defvar mail-mode-menu
  318.   '("Sendmail Commands"
  319.     "Sending Mail:"
  320.     "----"
  321.     ["Send and Exit"        mail-send-and-exit        t]
  322.     ["Send Mail"        mail-send            t]
  323.     "----"
  324.     "Go to Field:"
  325.     "----"
  326.     ["To:"            mail-to                t]
  327.     ["Subject:"            mail-subject            t]
  328.     ["CC:"            mail-cc                t]
  329.     ["BCC:"            mail-bcc            t]
  330.     ["Reply-To:"        mail-replyto            t]
  331. ;;    ["Sent Via:"        mail-sent-via            t]
  332.     ["Text"            mail-text            t]
  333.     "----"
  334.     "Miscellaneous Commands:"
  335.     "----"
  336.     ["Yank Original"        mail-yank-original        t]
  337.     ["Fill Yanked Message"    mail-fill-yanked-message    t]
  338.     ["Insert Signature"        mail-signature            t]
  339.     ["Insert File..."        insert-file            t]
  340.     ["Insert Buffer..."        insert-buffer            t]
  341.     "----"
  342.     ["Abort" kill-buffer t]
  343.     )
  344.   "Popup menu called by the function `mail-mode-menu'.")
  345.  
  346. (defun mail-mode-menu (event)
  347.   "Pop up the mail mode menu, defined by the variable `mail-mode-menu'."
  348.   (interactive "e")
  349.   (select-window (event-window event))
  350.   ;; Correctly sensitize the "Yank Original" and "Insert Signature" items.
  351.   (let (yank sig fill (rest mail-mode-menu))
  352.     (while rest
  353.       (if (vectorp (car rest))
  354.       (cond ((eq (aref (car rest) 1) 'mail-yank-original)
  355.          (setq yank (car rest)))
  356.         ((eq (aref (car rest) 1) 'mail-signature)
  357.          (setq sig (car rest)))
  358.         ((eq (aref (car rest) 1) 'mail-fill-yanked-message)
  359.          (setq fill (car rest)))))
  360.       (setq rest (cdr rest)))
  361.     (if yank (aset yank 2 (not (null mail-reply-buffer))))
  362.     (if sig (aset sig 2 (and (stringp mail-signature-file)
  363.                  (file-exists-p mail-signature-file))))
  364.     (if fill (aset fill 2
  365.            (save-excursion
  366.              (goto-char (point-min))
  367.              (and (search-forward (concat "\n" mail-header-separator
  368.                           "\n") nil t)
  369.               (not (looking-at "[ \t\n]*\\'"))))))
  370.     )
  371.   (let ((popup-menu-titles nil))
  372.     (popup-menu 'mail-mode-menu)))
  373.  
  374.  
  375. (defun mail-send-and-exit (arg)
  376.   "Send message like `mail-send', then, if no errors, exit from mail buffer.
  377. Prefix arg means don't delete this window."
  378.   (interactive "P")
  379.   (mail-send)
  380.   (mail-bury arg))
  381.  
  382. (defun mail-dont-send (arg)
  383.   "Don't send the message you have been editing.
  384. Prefix arg means don't delete this window."
  385.   (interactive "P")
  386.   (mail-bury arg))
  387.  
  388. (defun mail-bury (arg)
  389.   "Bury this mail buffer."
  390.   (let ((newbuf (other-buffer (current-buffer))))
  391.     (bury-buffer (current-buffer))
  392.     (cond ;;((and (fboundp 'frame-parameters)
  393.           ;;      (cdr (assq 'dedicated (frame-parameters)))
  394.           ;;      (not (null (delq (selected-frame) (visible-frame-list)))))
  395.           ;; (delete-frame (selected-frame)))
  396.           ((and (not arg)
  397.                 (not (one-window-p))
  398.                 (save-excursion
  399.                   (set-buffer (window-buffer (next-window (selected-window)
  400.                                                           'not)))
  401.                   (eq major-mode 'rmail-mode)))
  402.            (delete-window))
  403.           (t
  404.            (switch-to-buffer newbuf)))))
  405.  
  406. (defun mail-send ()
  407.   "Send the message in the current buffer.
  408.  
  409. If the file named by the variable `mail-signature-file' exists and
  410. the variable `mail-insert-signature' is non-nil, that file is inserted
  411. at the end.  If `mail-insert-signature' is nil, your .signature file
  412. will not be inserted unless you do it explicitly with C-c C-w.
  413.  
  414. If `mail-interactive' is non-nil, wait for success indication or error
  415. messages, and inform user.  Otherwise any failure is reported in a message
  416. back to the user from the mailer."
  417.   (interactive)
  418.   (if (cond (buffer-file-name
  419.              (y-or-n-p "Send buffer contents as mail message? "))
  420.             ((buffer-modified-p)
  421.              t)
  422.             (t
  423.              (y-or-n-p "Message already sent; resend? ")))
  424.       (progn
  425.     (message "Sending...")
  426.     (expand-abbrev)            ; for mail-abbrevs
  427.     (and (not mail-signature-inserted)
  428.          mail-insert-signature
  429.          (file-exists-p mail-signature-file)
  430.          (mail-signature))
  431.     (run-hooks 'mail-send-hook)
  432.     (funcall send-mail-function)
  433.     ;; Now perform actions on successful sending.
  434.     (while mail-send-actions
  435.       (condition-case nil
  436.           (apply (car (car mail-send-actions)) (cdr (car mail-send-actions)))
  437.         (error))
  438.       (setq mail-send-actions (cdr mail-send-actions)))
  439.     (message "Sending...done")
  440.  
  441.         ;; If buffer has no file, mark it as unmodified and delete autosave.
  442.         (cond ((or (not buffer-file-name)
  443.                    (not (buffer-modified-p)))
  444.                (set-buffer-modified-p nil)
  445.                (delete-auto-save-file-if-necessary t))
  446.               ((or noninteractive
  447.                    (y-or-n-p (format "Save file %s? " buffer-file-name)))
  448.                (save-buffer))))))
  449.  
  450. (defun sendmail-send-it ()
  451.   (let ((errbuf (if mail-interactive
  452.             (generate-new-buffer " sendmail errors")
  453.           0))
  454.     (tembuf (generate-new-buffer " sendmail temp"))
  455.     (case-fold-search nil)
  456.     delimline
  457.     (mailbuf (current-buffer)))
  458.     (unwind-protect
  459.     (save-excursion
  460.       (set-buffer tembuf)
  461.       (erase-buffer)
  462.       (insert-buffer-substring mailbuf)
  463.       (goto-char (point-max))
  464.       ;; require one newline at the end.
  465.       (or (= (preceding-char) ?\n)
  466.           (insert ?\n))
  467.       ;; Change header-delimiter to be what sendmail expects.
  468.       (goto-char (point-min))
  469.       (re-search-forward
  470.         (concat "^" (regexp-quote mail-header-separator) "\n"))
  471.       (replace-match "\n")
  472.       (backward-char 1)
  473.       (setq delimline (point-marker))
  474.       ;;(if mail-aliases ;; FSFmacs
  475.       ;;    (expand-mail-aliases (point-min) delimline))
  476.       (goto-char (point-min))
  477.       ;; ignore any blank lines in the header
  478.       (while (and (re-search-forward "\n\n\n*" delimline t)
  479.               (< (point) delimline))
  480.         (replace-match "\n"))
  481.       (let ((case-fold-search t))
  482.         (goto-char (point-min))
  483. ;; This is incorrect, RFC822 allows this.  -jwz
  484. ;;        (if (re-search-forward "^Sender:" delimline t)
  485. ;;        (error "Sender may not be specified."))
  486.         ;; Find and handle any FCC fields.
  487.         (goto-char (point-min))
  488.         (if (re-search-forward "^FCC:" delimline t)
  489.         (mail-do-fcc delimline))
  490. ;;; Apparently this causes a duplicate Sender.
  491. ;        ;; If the From is different than current user, insert Sender.
  492. ;        (goto-char (point-min))
  493. ;        (and (re-search-forward "^From:"  delimline t)
  494. ;         (progn
  495. ;           (require 'mail-utils)
  496. ;           (not (string-equal
  497. ;             (mail-strip-quoted-names
  498. ;              (save-restriction
  499. ;                (narrow-to-region (point-min) delimline)
  500. ;                (mail-fetch-field "From")))
  501. ;             (user-login-name))))
  502. ;         (progn
  503. ;           (forward-line 1)
  504. ;           (insert "Sender: " (user-login-name) "\n")))
  505. ;;; RMS garbage.  "S:" means "S:"
  506. ;        ;; "S:" is an abbreviation for "Subject:".
  507. ;        (goto-char (point-min))
  508. ;        (if (re-search-forward "^S:" delimline t)
  509. ;        (replace-match "Subject:"))
  510.         ;; Don't send out a blank subject line
  511.         (goto-char (point-min))
  512.         (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
  513.         (replace-match ""))
  514.         (if mail-interactive
  515.         (save-excursion
  516.           (set-buffer errbuf)
  517.           (erase-buffer))))
  518.       (apply 'call-process-region
  519.          (append (list (point-min) (point-max)
  520.                    (if (boundp 'sendmail-program)
  521.                    sendmail-program
  522.                  "/usr/lib/sendmail")
  523.                    nil errbuf nil
  524.                    "-oi" "-t")
  525.              ;; Always specify who from,
  526.              ;; since some systems have broken sendmails.
  527.              (list "-f" (user-login-name))
  528. ;;;             ;; Don't say "from root" if running under su.
  529. ;;;             (and (equal (user-real-login-name) "root")
  530. ;;;                  (list "-f" (user-login-name)))
  531.              ;;(and mail-alias-file ;; FSFmacs
  532.              ;;     (list (concat "-oA" mail-alias-file)))
  533.  
  534.              ;; These mean "report errors by mail"
  535.              ;; and "deliver in background".
  536.              (if (null mail-interactive) '("-oem" "-odb"))))
  537.       (if mail-interactive
  538.           (save-excursion
  539.         (set-buffer errbuf)
  540.         (goto-char (point-min))
  541.         (while (re-search-forward "\n\n* *" nil t)
  542.           (replace-match "; "))
  543.         (if (not (zerop (buffer-size)))
  544.             (error "Sending...failed to %s"
  545.                (buffer-substring (point-min) (point-max)))))))
  546.       (kill-buffer tembuf)
  547.       (if (bufferp errbuf)
  548.       (kill-buffer errbuf)))))
  549.  
  550. ;;(defun mail-sent-via ()
  551. ;;  "Make a Sent-via header line from each To or CC header line."
  552. ;;  (interactive)
  553. ;;  (save-excursion
  554. ;;  (goto-char (point-min))
  555. ;;  ;; find the header-separator
  556. ;;  (search-forward (concat "\n" mail-header-separator "\n"))
  557. ;;  (forward-line -1)
  558. ;;  ;; put a marker at the end of the header
  559. ;;  (let ((end (point-marker))
  560. ;;        (case-fold-search t)
  561. ;;        to-line)
  562. ;;    (goto-char (point-min))
  563. ;;    ;; search for the To: lines and make Sent-via: lines from them
  564. ;;    ;; search for the next To: line
  565. ;;    (while (re-search-forward "^\\(to\\|cc\\):" end t)
  566. ;;      ;; Grab this line plus all its continuations, sans the `to:'.
  567. ;;      (let ((to-line
  568. ;;             (buffer-substring (point)
  569. ;;                               (progn
  570. ;;                                 (if (re-search-forward "^[^ \t\n]" end t)
  571. ;;                                     (backward-char 1)
  572. ;;                                   (goto-char end))
  573. ;;                   (point)))))
  574. ;;      ;; Insert a copy, with altered header field name.
  575. ;;      (insert-before-markers "Sent-via:" to-line))))))
  576.  
  577. (defun mail-to ()
  578.   "Move point to end of To-field."
  579.   (interactive)
  580.   (expand-abbrev)
  581.   (mail-position-on-field "To"))
  582.  
  583. (defun mail-subject ()
  584.   "Move point to end of Subject-field."
  585.   (interactive)
  586.   (expand-abbrev)
  587.   (mail-position-on-field "Subject"))
  588.  
  589. (defun mail-cc ()
  590.   "Move point to end of CC-field.  Create a CC field if none."
  591.   (interactive)
  592.   (expand-abbrev)
  593.   (or (mail-position-on-field "cc" t)
  594.       (progn (mail-position-on-field "to")
  595.          (insert "\nCC: "))))
  596.  
  597. (defun mail-replyto ()
  598.   "Move point to end of Reply-To-field.  Create a Reply-To field if none."
  599.   (interactive)
  600.   (expand-abbrev)
  601.   (or (mail-position-on-field "reply-to" t)
  602.       (progn (mail-position-on-field "to")
  603.          (insert "\nReply-To: "))))
  604.  
  605. (defun mail-bcc ()
  606.   "Move point to end of BCC-field.  Create a BCC field if none."
  607.   (interactive)
  608.   (expand-abbrev)
  609.   (or (mail-position-on-field "bcc" t)
  610.       (progn (mail-position-on-field "to")
  611.          (insert "\nBCC: "))))
  612.  
  613. (defun mail-fcc ()
  614.   "Add a new FCC field, with file name completion."
  615.   (interactive)
  616.   (expand-abbrev)
  617.   (or (mail-position-on-field "fcc" t)    ;Put new field after exiting FCC.
  618.       (mail-position-on-field "to"))
  619.   (insert "\nFCC: " (read-file-name "Folder carbon copy: ")))
  620.  
  621. (defun mail-position-on-field (field &optional soft)
  622.   (let (end
  623.     (case-fold-search t))
  624.     (goto-char (point-min))
  625.     (search-forward (concat "\n" mail-header-separator "\n"))
  626.     (setq end (match-beginning 0))
  627.     (goto-char (point-min))
  628.     (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
  629.     (progn
  630.       (re-search-forward "^[^ \t]" nil 'move)
  631.       (beginning-of-line)
  632.       (skip-chars-backward "\n")
  633.       t)
  634.       (or soft
  635.       (progn (goto-char end)
  636.          (skip-chars-backward "\n")
  637.          (insert "\n" field ": ")))
  638.       nil)))
  639.  
  640. (defun mail-text ()
  641.   "Move point to beginning of text field."
  642.   (interactive)
  643.   (goto-char (point-min))
  644.   (search-forward (concat "\n" mail-header-separator "\n")))
  645.  
  646. (defun mail-signature (&optional atpoint)
  647.   "Sign letter with contents of `mail-signature-file'."
  648.   (interactive "P")
  649.   (save-excursion
  650.     (or atpoint
  651.     (goto-char (point-max)))
  652.     (skip-chars-backward " \t\n")
  653.     (end-of-line)
  654.     (or atpoint
  655.         (delete-region (point) (point-max)))
  656.     (insert "\n\n") ;(insert "\n\n--\n")
  657.     (if (stringp mail-signature)
  658.         (insert mail-signature)
  659.         (insert-file-contents (expand-file-name mail-signature-file)))
  660.     (setq mail-signature-inserted t)))
  661.  
  662. (defun mail-fill-yanked-message (&optional justifyp)
  663.   "Fill the paragraphs of a message yanked into this one.
  664. Numeric argument means justify as well."
  665.   (interactive "P")
  666.   (save-excursion
  667.     (goto-char (point-min))
  668.     (search-forward (concat "\n" mail-header-separator "\n") nil t)
  669.     (fill-individual-paragraphs (point)
  670.                 (point-max)
  671.                 justifyp
  672.                 t)))
  673.  
  674. (defun mail-indent-citation ()
  675.   "Modify text just inserted from a message to be cited.
  676. The inserted text should be the region.
  677. When this function returns, the region is again around the modified text.
  678.  
  679. Normally, indent each nonblank line `mail-indentation-spaces' spaces.
  680. However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
  681.   (let ((start (point)))
  682.     (mail-yank-clear-headers start (mark t))
  683.     (if (null mail-yank-prefix)
  684.     (indent-rigidly start (mark t) mail-indentation-spaces)
  685.       (save-excursion
  686.     (goto-char start)
  687.     (while (< (point) (mark t))
  688.       (insert mail-yank-prefix)
  689.       (forward-line 1))))))
  690.  
  691. (defun mail-yank-original (arg)
  692.   "Insert the message being replied to, if any (in rmail).
  693. Puts point before the text and mark after.
  694. Normally, indents each nonblank line ARG spaces (default 3).
  695. However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
  696.  
  697. Just \\[universal-argument] as argument means don't indent, insert no prefix,
  698. and don't delete any header fields."
  699.   (interactive "P")
  700.   (if mail-reply-buffer
  701.       (let ((start (point))
  702.         (reader-buf mail-reply-buffer)
  703.         (reader-window (get-buffer-window mail-reply-buffer
  704.                           (selected-frame))))
  705.     (if reader-window
  706.         (delete-windows-on reader-buf))
  707.     (insert-buffer reader-buf)
  708.     (if (consp arg)
  709.         nil
  710.       (goto-char start)
  711.       (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
  712.                        mail-indentation-spaces)))
  713.         (cond (mail-citation-hook
  714.                    (run-hooks 'mail-citation-hook))
  715.                   (mail-yank-hooks
  716.                    (run-hooks 'mail-yank-hooks))
  717.                   (t
  718.                    (mail-indent-citation)))))
  719.     (let ((zmacs-regions nil))
  720.       (exchange-point-and-mark))
  721.     (if (not (eolp)) (insert ?\n)))))
  722.  
  723. (defun mail-yank-clear-headers (start end)
  724.   (save-excursion
  725.     (goto-char start)
  726.     (if (search-forward "\n\n" end t)
  727.     (save-restriction
  728.       (narrow-to-region start (point))
  729.       (goto-char start)
  730.       (while (let ((case-fold-search t))
  731.            (re-search-forward mail-yank-ignored-headers nil t))
  732.         (beginning-of-line)
  733.         (delete-region (point)
  734.                (progn (re-search-forward "\n[^ \t]")
  735.                   (forward-char -1)
  736.                   (point))))))))
  737.  
  738.  
  739. ;;; FCC hackery, by jwz.  This version works on BABYL and VM buffers.
  740. ;;; To accomplish the latter, VM is loaded when this file is compiled.
  741. ;;; Don't worry, it's only loaded at compile-time.
  742.  
  743. (defun mail-do-fcc (header-end)
  744.   (let (fcc-list
  745.     (send-mail-buffer (current-buffer))
  746.     (tembuf (generate-new-buffer " rmail output"))
  747.     (case-fold-search t)
  748.     beg end)
  749.     (or (markerp header-end) (error "header-end must be a marker"))
  750.     (save-excursion
  751.       (goto-char (point-min))
  752.       (while (re-search-forward "^FCC:[ \t]*" header-end t)
  753.     (setq fcc-list (cons (buffer-substring (point)
  754.                            (progn
  755.                          (end-of-line)
  756.                          (skip-chars-backward " \t")
  757.                          (point)))
  758.                  fcc-list))
  759.     (delete-region (match-beginning 0)
  760.                (progn (forward-line 1) (point))))
  761.       (set-buffer tembuf)
  762.       (erase-buffer)
  763.       ;; insert just the headers to avoid moving the gap more than
  764.       ;; necessary (the message body could be arbitrarily huge.)
  765.       (insert-buffer-substring send-mail-buffer 1 header-end)
  766.  
  767.       ;; if there's no From: or Date: field, cons some.
  768.       (goto-char (point-min))
  769.       (or (re-search-forward "^From[ \t]*:" header-end t)
  770.       (insert "From: " (user-login-name) " (" (user-full-name) ")\n"))
  771.       (goto-char (point-min))
  772.       (or (re-search-forward "^Date[ \t]*:" header-end t)
  773.       (mail-do-fcc-insert-date-header))
  774.  
  775.       ;; insert a magic From_ line.
  776.       (goto-char (point-min))
  777.       (insert "\nFrom " (user-login-name) " " (current-time-string) "\n")
  778.       (goto-char (point-max))
  779.       (insert-buffer-substring send-mail-buffer header-end)
  780.       (goto-char (point-max))
  781.       (insert ?\n)
  782.       (goto-char (1- header-end))
  783.  
  784.       ;; ``Quote'' "^From " as ">From "
  785.       ;;  (note that this isn't really quoting, as there is no requirement
  786.       ;;   that "^[>]+From " be quoted in the same transparent way.)
  787.       (let ((case-fold-search nil))
  788.     (while (search-forward "\nFrom " nil t)
  789.       (forward-char -5)
  790.       (insert ?>)))
  791.  
  792.       (setq beg (point-min)
  793.         end (point-max))
  794.       (while fcc-list
  795.     (let ((target-buffer (get-file-buffer (car fcc-list))))
  796.       (if target-buffer
  797.           ;; File is present in a buffer => append to that buffer.
  798.           (save-excursion
  799.         (set-buffer target-buffer)
  800.         (cond ((eq major-mode 'rmail-mode)
  801.                (mail-do-fcc-rmail-internal tembuf))
  802.               ((eq major-mode 'vm-mode)
  803.                (mail-do-fcc-vm-internal tembuf))
  804.               (t
  805.                ;; Append to an ordinary buffer as a Unix mail message.
  806.                (goto-char (point-max))
  807.                (insert-buffer-substring tembuf beg end))))
  808.         ;; Else append to the file directly.
  809.         ;; (It's OK if it is an RMAIL or VM file -- the message will be
  810.         ;; parsed when the file is read in.)
  811.         (write-region
  812.          ;; Include a blank line before if file already exists.
  813.          (if (file-exists-p (car fcc-list)) (point-min) (1+ (point-min)))
  814.          (point-max) (car fcc-list) t)))
  815.     (setq fcc-list (cdr fcc-list))))
  816.     (kill-buffer tembuf)))
  817.  
  818. (defvar mail-do-fcc-cached-timezone nil)
  819.  
  820. (defun mail-do-fcc-insert-date-header ()
  821.   ;; Convert the ctime() format that `current-time-string' returns into
  822.   ;; an RFC-822-legal date.  
  823.   (let ((s (current-time-string)))
  824.     (string-match "\\`\\([A-Z][a-z][a-z]\\) +\\([A-Z][a-z][a-z]\\) +\\([0-9][0-9]?\\) *\\([0-9][0-9]?:[0-9][0-9]:[0-9][0-9]\\) *[0-9]?[0-9]?\\([0-9][0-9]\\)"
  825.           s)
  826.     (insert "Date: "
  827.         (substring s (match-beginning 1) (match-end 1)) ", "
  828.         (substring s (match-beginning 3) (match-end 3)) " "
  829.         (substring s (match-beginning 2) (match-end 2)) " "
  830.         (substring s (match-beginning 5) (match-end 5)) " "
  831.         (substring s (match-beginning 4) (match-end 4)) " ")
  832.  
  833.     (if mail-do-fcc-cached-timezone
  834.     (insert mail-do-fcc-cached-timezone "\n")
  835.       ;;
  836.       ;; First, try to use the current-time-zone function, which may not be
  837.       ;; defined, and even if it is defined, may error or return nil.
  838.       ;;
  839.       (or (condition-case ()
  840.           (let ((zoneinfo (current-time-zone)))
  841.         (setq mail-do-fcc-cached-timezone
  842.               (if (stringp (nth 1 zoneinfo))
  843.               (nth 1 zoneinfo)
  844.             (or (if (nth 1 zoneinfo) (nth 3 zoneinfo))
  845.                 (nth 2 zoneinfo))))
  846.         (if mail-do-fcc-cached-timezone
  847.             (insert mail-do-fcc-cached-timezone "\n"))
  848.         mail-do-fcc-cached-timezone)
  849.         (error nil))
  850.       ;;
  851.       ;; Otherwise, run date(1) and parse its output.  Yuck!
  852.       ;;
  853.       (save-restriction
  854.         (narrow-to-region (point) (point))
  855.         (call-process "date" nil t nil)
  856.         (end-of-line)
  857.         (insert "\n")
  858.         (forward-word -1)        ; skip back over year
  859.         (delete-region (1- (point)) (1- (point-max))) ; nuke year to end
  860.         (forward-word -1)        ; skip back over zone
  861.         (delete-region (point-min) (point)) ; nuke beginning to zone
  862.         (setq mail-do-fcc-cached-timezone
  863.           (buffer-substring (point-min) (1- (point-max)))))))))
  864.  
  865. (defun mail-do-fcc-rmail-internal (buffer)
  866.   (or (eq major-mode 'rmail-mode) (error "this only works in rmail-mode"))
  867.   (let ((b (point-min))
  868.     (e (point-max))
  869.     (buffer-read-only nil))
  870.     (unwind-protect
  871.     (progn
  872.       (widen)
  873.       (goto-char (point-max))
  874.       ;; This forces RMAIL's message counters to be recomputed when the
  875.       ;; next RMAIL operation is done on the buffer.
  876.       ;; See rmail-maybe-set-message-counters.
  877.       (setq rmail-total-messages nil)
  878.       (insert "\^L\n0, unseen,,\n*** EOOH ***")
  879.       (insert-buffer-substring buffer)
  880.       (insert "\n\C-_"))
  881.       (narrow-to-region b e)
  882.       (rmail-maybe-set-message-counters))))
  883.  
  884. ;;; Load VM into the compilation environment but not the load environment.
  885. (eval-when-compile
  886.  (or (and (boundp 'loading-vm-kludge) loading-vm-kludge)
  887.      ;; nastiness to avoid circular provide/require dependency nonsense
  888.      (fboundp 'vm-spool-files)
  889.      (let ((loading-vm-kludge t))
  890.        (require 'vm))))
  891.  
  892. (defun mail-do-fcc-vm-internal (buffer)
  893.   (or (eq major-mode 'vm-mode) (error "this only works in vm-mode"))
  894.   (let ((buffer-read-only nil)
  895.     (foreign-folder-p (not (eq vm-folder-type 'From_))))
  896.  
  897.     (if foreign-folder-p
  898.     ;; `buffer' has already been prepared with a "From " line which
  899.     ;; has a sensible user-id and date in it, but if we're FCCing to
  900.     ;; a VM folder that isn't in From_ format, we must discard that
  901.     ;; and let VM do whatever voodoo it needs to do.  (Actually we
  902.     ;; could do this all the time, but then all FCCed messages would
  903.     ;; have "From VM ..." envelopes, which is less attractive.)
  904.     (save-excursion
  905.       (set-buffer buffer)
  906.       (goto-char (point-min))
  907.       (skip-chars-forward "\n")
  908.       (forward-line)
  909.       (delete-region (point-min) (point))))
  910.  
  911.     ;; Largely copied from #'vm-save-message in vm-save.el
  912.     (vm-save-restriction
  913.      (widen)
  914.      (goto-char (point-max))
  915.      (if foreign-folder-p
  916.      (vm-write-string (current-buffer)
  917.               (vm-leading-message-separator vm-folder-type)))
  918.      (insert-buffer-substring buffer)
  919.      (if foreign-folder-p
  920.      (vm-write-string (current-buffer)
  921.               (vm-trailing-message-separator vm-folder-type)))
  922.  
  923.      (vm-increment vm-messages-not-on-disk)
  924.      (vm-set-buffer-modified-p t)
  925.      (vm-clear-modification-flag-undos)
  926.      (vm-check-for-killed-summary)
  927.      (vm-assimilate-new-messages)
  928.      (vm-update-summary-and-mode-line))))
  929.  
  930.  
  931. ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
  932.  
  933. ;;;###autoload
  934. (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
  935.   "Edit a message to be sent.  Prefix arg means resume editing (don't erase).
  936. When this function returns, the buffer `*mail*' is selected.
  937. The value is t if the message was newly initialized; otherwise, nil.
  938.  
  939. \\<mail-mode-map>
  940. While editing message, type \\[mail-send-and-exit] to send the message and exit.
  941.  
  942. Various special commands starting with C-c are available in sendmail mode
  943. to move to message header fields:
  944. \\{mail-mode-map}
  945.  
  946. The variable `mail-signature' controls whether the signature file
  947. `mail-signature-file' is inserted immediately.
  948.  
  949. If `mail-insert-signature' is non-nil, the signature file, denoted by
  950. the variable `mail-signature-file', is automatically inserted at the
  951. end of the message before sending.  (Otherwise use C-c C-w).
  952.  
  953. If `mail-self-blind' is non-nil, a BCC to yourself is inserted
  954. when the message is initialized.
  955.  
  956. If `mail-default-reply-to' is non-nil, it should be an address (a string);
  957. a Reply-to: field with that address is inserted.
  958.  
  959. If `mail-archive-file-name' is non-nil, an FCC field with that file name
  960. is inserted.
  961.  
  962. If `mail-setup-hook' is bound, its value is called with no arguments
  963. after the message is initialized.  It can add more default fields.
  964.  
  965. When calling from a program, the second through fifth arguments
  966.  TO, SUBJECT, IN-REPLY-TO and CC specify if non-nil
  967.  the initial contents of those header fields.
  968.  These arguments should not have final newlines.
  969. The sixth argument REPLYBUFFER is a buffer whose contents
  970.  should be yanked if the user types C-c C-y.
  971. The seventh argument ACTIONS is a list of actions to take
  972.  if/when the message is sent.  Each action looks like (FUNCTION . ARGS);
  973.  when the message is sent, we apply FUNCTION to ARGS.
  974.  This is how Rmail arranges to mark messages `answered'."
  975.   (interactive "P")
  976.   (let ((index 1)
  977.     buffer)
  978.     ;; If requested, look for a mail buffer that is modified and go to it.
  979.     (if noerase
  980.     (progn
  981.       (while (and (setq buffer
  982.                 (get-buffer (if (= 1 index) "*mail*"
  983.                       (format "*mail*<%d>" index))))
  984.               (not (buffer-modified-p buffer)))
  985.         (setq index (1+ index)))
  986.       (if buffer (switch-to-buffer buffer)
  987.         ;; If none exists, start a new message.
  988.         ;; This will never re-use an existing unmodified mail buffer
  989.         ;; (since index is not 1 anymore).  Perhaps it should.
  990.         (setq noerase nil))))
  991.     ;; Unless we found a modified message and are happy, start a new message.
  992.     (if (not noerase)
  993.     (progn
  994.       ;; Look for existing unmodified mail buffer.
  995.       (while (and (setq buffer
  996.                 (get-buffer (if (= 1 index) "*mail*"
  997.                       (format "*mail*<%d>" index))))
  998.               (buffer-modified-p buffer))
  999.         (setq index (1+ index)))
  1000.       ;; If none, make a new one.
  1001.       (or buffer
  1002.           (setq buffer (generate-new-buffer "*mail*")))
  1003.       ;; Go there and initialize it.
  1004.       (switch-to-buffer buffer)
  1005.       (erase-buffer)
  1006.       ;; put mail auto-save files in home dir instead of
  1007.       ;; scattering them around the file system.
  1008.           (setq default-directory (or mail-dir (expand-file-name "~/")))
  1009.           (auto-save-mode auto-save-default)
  1010.           (mail-mode)
  1011.           (mail-setup to subject in-reply-to cc replybuffer actions)
  1012.       (if (and buffer-auto-save-file-name
  1013.            (file-exists-p buffer-auto-save-file-name))
  1014.           (message "Auto save file for draft message exists; consider M-x mail-recover"))
  1015.           t))))
  1016.  
  1017. (defun mail-recover ()
  1018.   "Reread contents of current buffer from its last auto-save file."
  1019.   (interactive)
  1020.   (let ((file-name (let ((default-directory (expand-file-name "~/")))
  1021.              ;; put mail auto-save files in home dir instead of
  1022.              ;; scattering them around the file system.
  1023.              (make-auto-save-file-name))))
  1024.     (cond ((save-window-excursion
  1025.          (if (not (eq system-type 'vax-vms))
  1026.          (with-output-to-temp-buffer "*Directory*"
  1027.            (buffer-disable-undo standard-output)
  1028.            (call-process "ls" nil standard-output nil "-l" file-name)))
  1029.          (yes-or-no-p (format "Recover auto save file %s? " file-name)))
  1030.        (let ((buffer-read-only nil))
  1031.          (erase-buffer)
  1032.          (insert-file-contents file-name nil)))
  1033.       (t (error "mail-recover cancelled.")))))
  1034.  
  1035. ;;;###autoload
  1036. (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
  1037.   "Like `mail' command, but display mail buffer in another window."
  1038.   (interactive "P")
  1039.   (let ((pop-up-windows t))
  1040.     (pop-to-buffer "*mail*"))
  1041.   (mail noerase to subject in-reply-to cc replybuffer sendactions))
  1042.  
  1043. ;;;###autoload
  1044. (defun mail-other-frame (&optional noerase to subject in-reply-to cc
  1045.                     replybuffer sendactions)
  1046.   "Like `mail' command, but display mail buffer in another frame."
  1047.   (interactive "P")
  1048.   (switch-to-buffer-other-frame (get-buffer-create "*mail*"))
  1049.   (mail noerase to subject in-reply-to cc replybuffer sendactions))
  1050.  
  1051. ;;; Do not add anything but external entries on this page.
  1052.  
  1053. (provide 'sendmail)
  1054.  
  1055. ;;; sendmail.el ends here
  1056.